<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>后台管理</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
            background-image: url("background.png");
            background-size: cover;
            background-position-x: center;
            background-repeat: no-repeat;
        }
        .container {
            background-color: #ffffffc3;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 80%;
            max-width: 600px;
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        h1 {
            text-align: center;
        }
        button {
            display: block;
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: none;
            border-radius: 5px;
            background-color: #468ef3;
            color: white;
            font-size: 16px;
            cursor: pointer;
        }
        button:hover {
            background-color: #669fee;
        }
        input[type="file"], input[type="text"], input[type="password"] {
            display: block;
            margin: 10px 0;
        }
        #serverStatus {
            text-align: center;
            margin: 20px 0;
            font-size: 18px;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>后台管理</h1>
        <div id="serverStatus">服务器状态: 未知</div>
        <button id="startServerBtn">启动主站服务器</button>
        <button id="stopServerBtn">停止主站服务器</button>
        <form id="uploadForm" enctype="multipart/form-data" style="display: flex; flex-direction: column; align-items: center;">
            <input type="file" id="file" name="file" multiple required>
            <button type="submit">上传文件并更新主站</button>
        </form>
        <form id="mottoForm" style="display: flex; flex-direction: column; align-items: center;">
            <input type="text" id="motto" name="motto" placeholder="输入新的语录" required>
            <button type="submit">保存语录</button>
        </form>
        <form id="passwordForm" style="display: flex; flex-direction: column; align-items: center;">
            <input type="password" id="password" name="password" placeholder="输入管理员密码" required>
        </form>
    </div>
    <script>
        const serverStatus = document.getElementById('serverStatus');

        function updateServerStatus(status) {
            serverStatus.textContent = `服务器状态: ${status}`;
        }

        function getPassword() {
            return document.getElementById('password').value;
        }

        document.getElementById('startServerBtn').addEventListener('click', function() {
            fetch('/start-server', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ password: getPassword() })
            })
            .then(response => response.json())
            .then(data => {
                alert(data.message);
                if (data.message.includes('已启动')) {
                    updateServerStatus('运行中');
                }
            })
            .catch(error => console.error('Error:', error));
        });

        document.getElementById('stopServerBtn').addEventListener('click', function() {
            fetch('/stop-server', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ password: getPassword() })
            })
            .then(response => response.json())
            .then(data => {
                alert(data.message);
                if (data.message.includes('已停止')) {
                    updateServerStatus('已停止');
                }
            })
            .catch(error => console.error('Error:', error));
        });

        document.getElementById('uploadForm').addEventListener('submit', function(e) {
            e.preventDefault();
            var formData = new FormData(this);
            formData.append('password', getPassword());
            fetch('/upload-files', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => alert(data.message))
            .catch(error => console.error('Error:', error));
        });

        document.getElementById('mottoForm').addEventListener('submit', function(e) {
            e.preventDefault();
            var motto = document.getElementById('motto').value;
            fetch('/save-motto', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ motto: motto, password: getPassword() })
            })
            .then(response => response.json())
            .then(data => alert(data.message))
            .catch(error => console.error('Error:', error));
        });

        // WebSocket to receive real-time server status updates
        const socket = new WebSocket('ws://' + window.location.host + '/ws');
        socket.onmessage = function(event) {
            updateServerStatus(event.data);
        };

        // Fetch initial server status on page load
        fetch('/server-status')
            .then(response => response.json())
            .then(data => updateServerStatus(data.status))
            .catch(error => console.error('Error:', error));
    </script>
</body>
</html>